{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10 minutes to pandas\n",
    "## pandas 的数据类型\n",
    "\n",
    "## `Series` 序列\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       1\n",
      "1       4\n",
      "2       2\n",
      "3     aab\n",
      "4    3.23\n",
      "5     NaN\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sys\n",
    "sys.path.append(\"..\") \n",
    "\n",
    "\n",
    "\n",
    "s = pd.Series([1,4,2,'aab',3.23,np.nan])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Creating a `Series` by passing a list of values, letting pandas create a default integer index:\n",
    "\n",
    "通过一个List来创建序列 `Series`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1.00\n",
      "1    4.00\n",
      "2    2.00\n",
      "3    3.23\n",
      "4     NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "s = pd.Series([1,4,2,3.23,np.nan])\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `DataFrame`\n",
    "Creating a DataFrame by passing a NumPy array, with a datetime index and labeled columns:\n",
    "\n",
    "### `DataFrame` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2019-09-18', '2019-09-19', '2019-09-20', '2019-09-21',\n",
      "               '2019-09-22', '2019-09-23', '2019-09-24', '2019-09-25',\n",
      "               '2019-09-26', '2019-09-27', '2019-09-28', '2019-09-29'],\n",
      "              dtype='datetime64[ns]', freq='D')\n"
     ]
    }
   ],
   "source": [
    "dates = pd.date_range('20190918', periods = 12)\n",
    "print(dates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_frame = pd.DataFrame(np.random.normal(1, 0.01, (12,3)), index = dates, columns = list('ABC'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C\n",
      "2019-09-18  0.989936  0.990347  1.005533\n",
      "2019-09-19  0.984528  0.980366  1.018356\n",
      "2019-09-20  1.016322  1.003045  0.998361\n",
      "2019-09-21  0.993233  0.993873  0.992124\n",
      "2019-09-22  0.984321  0.998008  1.004511\n",
      "2019-09-23  0.994078  0.985869  0.990937\n",
      "2019-09-24  0.995265  1.009943  1.005325\n",
      "2019-09-25  0.997671  1.004346  1.010415\n",
      "2019-09-26  1.002895  1.021855  1.009793\n",
      "2019-09-27  1.001533  1.003061  1.012216\n",
      "2019-09-28  0.998256  0.984714  1.009659\n",
      "2019-09-29  1.020964  1.004581  1.005718\n"
     ]
    }
   ],
   "source": [
    "print(data_frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A          B    C  D      E    F\n",
      "0  1.0 2013-01-02  1.0  3   test  foo\n",
      "1  1.0 2013-01-02  1.0  3  train  foo\n",
      "2  1.0 2013-01-02  1.0  3   test  foo\n",
      "3  1.0 2013-01-02  1.0  3  train  foo\n",
      "A           float64\n",
      "B    datetime64[ns]\n",
      "C           float32\n",
      "D             int32\n",
      "E          category\n",
      "F            object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "#也可以用一个 dict 来初始化\n",
    "data_frame2 = pd.DataFrame({'A': 1.,\n",
    "                      'B': pd.Timestamp('20130102'),\n",
    "                      'C': pd.Series(1, index=list(range(4)), dtype='float32'),\n",
    "                    'D': np.array([3] * 4, dtype='int32'),\n",
    "                     'E': pd.Categorical([\"test\", \"train\", \"test\", \"train\"]),\n",
    "                    'F': 'foo'})\n",
    "print(data_frame2)\n",
    "print(data_frame2.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2013-01-02</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2013-01-02</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2013-01-02</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2013-01-02</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B    C  D      E    F\n",
       "0  1.0 2013-01-02  1.0  3   test  foo\n",
       "1  1.0 2013-01-02  1.0  3  train  foo\n",
       "2  1.0 2013-01-02  1.0  3   test  foo\n",
       "3  1.0 2013-01-02  1.0  3  train  foo"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([[1,3,6.0,2],[2,3,4,5]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 3., 6., 5.])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#不同维度最大值\n",
    "np.max(x, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看前面几行也最后几行数据view the top and bottom rows of the frame:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-18  0.989936  0.990347  1.005533\n",
       "2019-09-19  0.984528  0.980366  1.018356\n",
       "2019-09-20  1.016322  1.003045  0.998361\n",
       "2019-09-21  0.993233  0.993873  0.992124\n",
       "2019-09-22  0.984321  0.998008  1.004511"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2013-01-02</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2013-01-02</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A          B    C  D      E    F\n",
       "2  1.0 2013-01-02  1.0  3   test  foo\n",
       "3  1.0 2013-01-02  1.0  3  train  foo"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame2.tail(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "显示索引、列和底层的 numpy 数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([0, 1, 2, 3], dtype='int64')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame2.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame2.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DataFrame.to_numpy（）给出了底层数据的NumPy表示。 请注意，当您的DataFrame具有不同数据类型的列时，这可能是一项昂贵的操作，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],\n",
       "       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo'],\n",
       "       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],\n",
       "       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo']],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame2.to_numpy()\n",
    "#relatively expensive. with different data type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这归结为pandas和NumPy之间的根本区别：NumPy数组对整个数组有一个dtype，而pandas DataFrames每列有一个dtype。 当您调用DataFrame.to_numpy（）时，pandas将找到可以容纳DataFrame中所有dtypes的NumPy dtype。 这可能最终成为对象，这需要将每个值都转换为Python对象。 对于df，我们的所有浮点值的DataFrame，DataFrame.to_numpy（）都很快，不需要复制数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],\n",
       "       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo'],\n",
       "       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],\n",
       "       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo']],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame2.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.9899355 , 0.99034656, 1.00553281],\n",
       "       [0.98452766, 0.98036606, 1.01835587],\n",
       "       [1.01632168, 1.00304465, 0.99836147],\n",
       "       [0.99323278, 0.99387331, 0.99212415],\n",
       "       [0.98432102, 0.99800835, 1.00451057],\n",
       "       [0.99407823, 0.98586923, 0.99093663],\n",
       "       [0.99526502, 1.00994342, 1.00532531],\n",
       "       [0.99767057, 1.00434615, 1.01041453],\n",
       "       [1.00289541, 1.02185481, 1.00979347],\n",
       "       [1.00153282, 1.00306054, 1.01221594],\n",
       "       [0.99825626, 0.98471409, 1.00965897],\n",
       "       [1.02096449, 1.00458091, 1.00571811]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.to_numpy()\n",
    "#this is cheap, because floating-point value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2019-09-18 00:00:00</th>\n",
       "      <th>2019-09-19 00:00:00</th>\n",
       "      <th>2019-09-20 00:00:00</th>\n",
       "      <th>2019-09-21 00:00:00</th>\n",
       "      <th>2019-09-22 00:00:00</th>\n",
       "      <th>2019-09-23 00:00:00</th>\n",
       "      <th>2019-09-24 00:00:00</th>\n",
       "      <th>2019-09-25 00:00:00</th>\n",
       "      <th>2019-09-26 00:00:00</th>\n",
       "      <th>2019-09-27 00:00:00</th>\n",
       "      <th>2019-09-28 00:00:00</th>\n",
       "      <th>2019-09-29 00:00:00</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.984528</td>\n",
       "      <td>1.016322</td>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.994078</td>\n",
       "      <td>0.995265</td>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.001533</td>\n",
       "      <td>0.998256</td>\n",
       "      <td>1.020964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.990347</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>0.985869</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>0.984714</td>\n",
       "      <td>1.004581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.005533</td>\n",
       "      <td>1.018356</td>\n",
       "      <td>0.998361</td>\n",
       "      <td>0.992124</td>\n",
       "      <td>1.004511</td>\n",
       "      <td>0.990937</td>\n",
       "      <td>1.005325</td>\n",
       "      <td>1.010415</td>\n",
       "      <td>1.009793</td>\n",
       "      <td>1.012216</td>\n",
       "      <td>1.009659</td>\n",
       "      <td>1.005718</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   2019-09-18  2019-09-19  2019-09-20  2019-09-21  2019-09-22  2019-09-23  \\\n",
       "A    0.989936    0.984528    1.016322    0.993233    0.984321    0.994078   \n",
       "B    0.990347    0.980366    1.003045    0.993873    0.998008    0.985869   \n",
       "C    1.005533    1.018356    0.998361    0.992124    1.004511    0.990937   \n",
       "\n",
       "   2019-09-24  2019-09-25  2019-09-26  2019-09-27  2019-09-28  2019-09-29  \n",
       "A    0.995265    0.997671    1.002895    1.001533    0.998256    1.020964  \n",
       "B    1.009943    1.004346    1.021855    1.003061    0.984714    1.004581  \n",
       "C    1.005325    1.010415    1.009793    1.012216    1.009659    1.005718  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.T\n",
    "#转置数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.9899355 , 0.98452766, 1.01632168, 0.99323278, 0.98432102,\n",
       "        0.99407823, 0.99526502, 0.99767057, 1.00289541, 1.00153282,\n",
       "        0.99825626, 1.02096449],\n",
       "       [0.99034656, 0.98036606, 1.00304465, 0.99387331, 0.99800835,\n",
       "        0.98586923, 1.00994342, 1.00434615, 1.02185481, 1.00306054,\n",
       "        0.98471409, 1.00458091],\n",
       "       [1.00553281, 1.01835587, 0.99836147, 0.99212415, 1.00451057,\n",
       "        0.99093663, 1.00532531, 1.01041453, 1.00979347, 1.01221594,\n",
       "        1.00965897, 1.00571811]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.T.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>12.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>12.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.998250</td>\n",
       "      <td>0.998334</td>\n",
       "      <td>1.005246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.011193</td>\n",
       "      <td>0.011891</td>\n",
       "      <td>0.008048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>0.990937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.992408</td>\n",
       "      <td>0.989227</td>\n",
       "      <td>1.002973</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.996468</td>\n",
       "      <td>1.000527</td>\n",
       "      <td>1.005625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.001873</td>\n",
       "      <td>1.004405</td>\n",
       "      <td>1.009949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               A          B          C\n",
       "count  12.000000  12.000000  12.000000\n",
       "mean    0.998250   0.998334   1.005246\n",
       "std     0.011193   0.011891   0.008048\n",
       "min     0.984321   0.980366   0.990937\n",
       "25%     0.992408   0.989227   1.002973\n",
       "50%     0.996468   1.000527   1.005625\n",
       "75%     1.001873   1.004405   1.009949\n",
       "max     1.020964   1.021855   1.018356"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>0.998256</td>\n",
       "      <td>0.984714</td>\n",
       "      <td>1.009659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>0.995265</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.005325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>0.994078</td>\n",
       "      <td>0.985869</td>\n",
       "      <td>0.990937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-29  1.020964  1.004581  1.005718\n",
       "2019-09-28  0.998256  0.984714  1.009659\n",
       "2019-09-27  1.001533  1.003061  1.012216\n",
       "2019-09-26  1.002895  1.021855  1.009793\n",
       "2019-09-25  0.997671  1.004346  1.010415\n",
       "2019-09-24  0.995265  1.009943  1.005325\n",
       "2019-09-23  0.994078  0.985869  0.990937\n",
       "2019-09-22  0.984321  0.998008  1.004511\n",
       "2019-09-21  0.993233  0.993873  0.992124\n",
       "2019-09-20  1.016322  1.003045  0.998361\n",
       "2019-09-19  0.984528  0.980366  1.018356\n",
       "2019-09-18  0.989936  0.990347  1.005533"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.sort_index(axis = 0, ascending= False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>0.994078</td>\n",
       "      <td>0.985869</td>\n",
       "      <td>0.990937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>0.995265</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.005325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>0.998256</td>\n",
       "      <td>0.984714</td>\n",
       "      <td>1.009659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-22  0.984321  0.998008  1.004511\n",
       "2019-09-19  0.984528  0.980366  1.018356\n",
       "2019-09-18  0.989936  0.990347  1.005533\n",
       "2019-09-21  0.993233  0.993873  0.992124\n",
       "2019-09-23  0.994078  0.985869  0.990937\n",
       "2019-09-24  0.995265  1.009943  1.005325\n",
       "2019-09-25  0.997671  1.004346  1.010415\n",
       "2019-09-28  0.998256  0.984714  1.009659\n",
       "2019-09-27  1.001533  1.003061  1.012216\n",
       "2019-09-26  1.002895  1.021855  1.009793\n",
       "2019-09-20  1.016322  1.003045  0.998361\n",
       "2019-09-29  1.020964  1.004581  1.005718"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.sort_values(by = 'A',ascending = True)\n",
    "#通过某一个特征维度进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>0.994078</td>\n",
       "      <td>0.985869</td>\n",
       "      <td>0.990937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>0.995265</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.005325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>0.998256</td>\n",
       "      <td>0.984714</td>\n",
       "      <td>1.009659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-18  0.989936  0.990347  1.005533\n",
       "2019-09-19  0.984528  0.980366  1.018356\n",
       "2019-09-20  1.016322  1.003045  0.998361\n",
       "2019-09-21  0.993233  0.993873  0.992124\n",
       "2019-09-22  0.984321  0.998008  1.004511\n",
       "2019-09-23  0.994078  0.985869  0.990937\n",
       "2019-09-24  0.995265  1.009943  1.005325\n",
       "2019-09-25  0.997671  1.004346  1.010415\n",
       "2019-09-26  1.002895  1.021855  1.009793\n",
       "2019-09-27  1.001533  1.003061  1.012216\n",
       "2019-09-28  0.998256  0.984714  1.009659\n",
       "2019-09-29  1.020964  1.004581  1.005718"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selection\n",
    "\n",
    "注意虽然用于选择和设置的标准Python / Numpy表达式非常直观并且对于交互式工作非常方便\n",
    "就是对于data[start: end  : step] 这样的操作，不是很pandasnic， 这个库自有一套操作。\n",
    "\n",
    "但对于生产代码，我们建议使用优化的pandas数据访问方法.at，.iat，.loc和.iloc。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-19  0.984528  0.980366  1.018356\n",
       "2019-09-20  1.016322  1.003045  0.998361"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-19  0.984528  0.980366  1.018356\n",
       "2019-09-22  0.984321  0.998008  1.004511\n",
       "2019-09-25  0.997671  1.004346  1.010415"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame[1:10:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.980366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.003045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.998008</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   B\n",
       "2019-09-19  0.980366\n",
       "2019-09-20  1.003045\n",
       "2019-09-21  0.993873\n",
       "2019-09-22  0.998008"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#data_frame[1:5, 1:2]\n",
    "\n",
    "#这样的pythonic的操作就不行了\n",
    "\n",
    "data_frame.iloc[1:5, 1:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-19    1.018356\n",
       "2019-09-20    0.998361\n",
       "2019-09-21    0.992124\n",
       "2019-09-22    1.004511\n",
       "Freq: D, Name: C, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.iloc[1:5, -1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-19  0.984528  0.980366  1.018356\n",
       "2019-09-20  1.016322  1.003045  0.998361\n",
       "2019-09-21  0.993233  0.993873  0.992124"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame['2019-09-19': '2019-09-21']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selection by labe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.989936\n",
       "B    0.990347\n",
       "C    1.005533\n",
       "Name: 2019-09-18 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.loc[dates[0]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Selection by position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-19    1.018356\n",
       "2019-09-20    0.998361\n",
       "2019-09-21    0.992124\n",
       "2019-09-22    1.004511\n",
       "Freq: D, Name: C, dtype: float64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.iloc[1:5, -1]\n",
    "#By integer slices, acting similar to numpy/python:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>1.004511</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         C\n",
       "2019-09-19  0.984528  1.018356\n",
       "2019-09-20  1.016322  0.998361\n",
       "2019-09-22  0.984321  1.004511"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#By lists of integer position locations, similar to the numpy/python style:\n",
    "data_frame.iloc[[1, 2, 4], [0, 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0183558683515073"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#For getting fast access to a scalar (equivalent to the prior method):\n",
    "#相当于df.iloc[1, 1]\n",
    "data_frame.iat[1,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## by Boolean indexing\n",
    "\n",
    "通过布尔 maks 来 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                A      B      C\n",
       "2019-09-18  False  False   True\n",
       "2019-09-19  False  False   True\n",
       "2019-09-20   True   True  False\n",
       "2019-09-21  False  False  False\n",
       "2019-09-22  False  False   True\n",
       "2019-09-23  False  False  False\n",
       "2019-09-24  False   True   True\n",
       "2019-09-25  False   True   True\n",
       "2019-09-26   True   True   True\n",
       "2019-09-27   True   True   True\n",
       "2019-09-28  False  False   True\n",
       "2019-09-29   True   True   True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.005533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.018356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.004511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.005325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.009659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-18       NaN       NaN  1.005533\n",
       "2019-09-19       NaN       NaN  1.018356\n",
       "2019-09-20  1.016322  1.003045       NaN\n",
       "2019-09-21       NaN       NaN       NaN\n",
       "2019-09-22       NaN       NaN  1.004511\n",
       "2019-09-23       NaN       NaN       NaN\n",
       "2019-09-24       NaN  1.009943  1.005325\n",
       "2019-09-25       NaN  1.004346  1.010415\n",
       "2019-09-26  1.002895  1.021855  1.009793\n",
       "2019-09-27  1.001533  1.003061  1.012216\n",
       "2019-09-28       NaN       NaN  1.009659\n",
       "2019-09-29  1.020964  1.004581  1.005718"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame[data_frame > 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-18    False\n",
       "2019-09-19    False\n",
       "2019-09-20     True\n",
       "2019-09-21    False\n",
       "2019-09-22    False\n",
       "2019-09-23    False\n",
       "2019-09-24    False\n",
       "2019-09-25    False\n",
       "2019-09-26     True\n",
       "2019-09-27     True\n",
       "2019-09-28    False\n",
       "2019-09-29     True\n",
       "Freq: D, Name: A, dtype: bool"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.A > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C\n",
       "2019-09-20  1.016322  1.003045  0.998361\n",
       "2019-09-26  1.002895  1.021855  1.009793\n",
       "2019-09-27  1.001533  1.003061  1.012216\n",
       "2019-09-29  1.020964  1.004581  1.005718"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame[data_frame.A > 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting 设置数值\n",
    "赋值的方式 at loc iat iloc\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = pd.Series(list(range(0,12)), index = dates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-18     0\n",
       "2019-09-19     1\n",
       "2019-09-20     2\n",
       "2019-09-21     3\n",
       "2019-09-22     4\n",
       "2019-09-23     5\n",
       "2019-09-24     6\n",
       "2019-09-25     7\n",
       "2019-09-26     8\n",
       "2019-09-27     9\n",
       "2019-09-28    10\n",
       "2019-09-29    11\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_frame['F'] = s1\n",
    "# setting new columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>0.994078</td>\n",
       "      <td>0.985869</td>\n",
       "      <td>0.990937</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>0.995265</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.005325</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>0.998256</td>\n",
       "      <td>0.984714</td>\n",
       "      <td>1.009659</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C   F\n",
       "2019-09-18  0.989936  0.990347  1.005533   0\n",
       "2019-09-19  0.984528  0.980366  1.018356   1\n",
       "2019-09-20  1.016322  1.003045  0.998361   2\n",
       "2019-09-21  0.993233  0.993873  0.992124   3\n",
       "2019-09-22  0.984321  0.998008  1.004511   4\n",
       "2019-09-23  0.994078  0.985869  0.990937   5\n",
       "2019-09-24  0.995265  1.009943  1.005325   6\n",
       "2019-09-25  0.997671  1.004346  1.010415   7\n",
       "2019-09-26  1.002895  1.021855  1.009793   8\n",
       "2019-09-27  1.001533  1.003061  1.012216   9\n",
       "2019-09-28  0.998256  0.984714  1.009659  10\n",
       "2019-09-29  1.020964  1.004581  1.005718  11"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "#data_frame.iat['F'][0] = np.nan\n",
    "data_frame.iloc[0,-1] = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting values by label:\n",
    "\n",
    "```python\n",
    "In [48]: df.at[dates[0], 'A'] = 0\n",
    "\n",
    "Setting values by position:\n",
    "\n",
    "In [49]: df.iat[0, 1] = 0\n",
    "    或者是 iloc？\n",
    "\n",
    "Setting by assigning with a NumPy array:\n",
    "\n",
    "In [50]: df.loc[:, 'D'] = np.array([5] * len(df))\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>0.994078</td>\n",
       "      <td>0.985869</td>\n",
       "      <td>0.990937</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>0.995265</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.005325</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>0.998256</td>\n",
       "      <td>0.984714</td>\n",
       "      <td>1.009659</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C     F\n",
       "2019-09-18  0.989936  0.990347  1.005533   NaN\n",
       "2019-09-19  0.984528  0.980366  1.018356   1.0\n",
       "2019-09-20  1.016322  1.003045  0.998361   2.0\n",
       "2019-09-21  0.993233  0.993873  0.992124   3.0\n",
       "2019-09-22  0.984321  0.998008  1.004511   4.0\n",
       "2019-09-23  0.994078  0.985869  0.990937   5.0\n",
       "2019-09-24  0.995265  1.009943  1.005325   6.0\n",
       "2019-09-25  0.997671  1.004346  1.010415   7.0\n",
       "2019-09-26  1.002895  1.021855  1.009793   8.0\n",
       "2019-09-27  1.001533  1.003061  1.012216   9.0\n",
       "2019-09-28  0.998256  0.984714  1.009659  10.0\n",
       "2019-09-29  1.020964  1.004581  1.005718  11.0"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 丢失数据的处理，数据清洗\n",
    "pandas主要使用值np.nan来表示缺失的数据。默认情况下，它不包含在计算中。\n",
    "\n",
    "Missing Data section. https://pandas.pydata.org/pandas-docs/stable/user_guide/missing_data.html#missing-data\n",
    "\n",
    "`reiindex`重建索引允许您更改/添加/删除指定轴上的索引。这将返回数据的副本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = data_frame.reindex(index=dates[0:4], columns=list(data_frame.columns) + ['E'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.loc[0:4:2, 'E'] = 1\n",
    "#loc at 可以lable index混用， iloc iat 只能纯index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C    F    E\n",
       "2019-09-18  0.989936  0.990347  1.005533  NaN  1.0\n",
       "2019-09-19  0.984528  0.980366  1.018356  1.0  NaN\n",
       "2019-09-20  1.016322  1.003045  0.998361  2.0  1.0\n",
       "2019-09-21  0.993233  0.993873  0.992124  3.0  NaN"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1\n",
    "#上面的data frame "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C    F    E\n",
       "2019-09-20  1.016322  1.003045  0.998361  2.0  1.0"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.dropna(how='any')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "      <td>1.11</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1.11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C     F     E\n",
       "2019-09-18  0.989936  0.990347  1.005533  1.11  1.00\n",
       "2019-09-19  0.984528  0.980366  1.018356  1.00  1.11\n",
       "2019-09-20  1.016322  1.003045  0.998361  2.00  1.00\n",
       "2019-09-21  0.993233  0.993873  0.992124  3.00  1.11"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.fillna(value=1.11)\n",
    "#用 一个数值来填充 NAN值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                A      B      C      F      E\n",
       "2019-09-18  False  False  False   True  False\n",
       "2019-09-19  False  False  False  False   True\n",
       "2019-09-20  False  False  False  False  False\n",
       "2019-09-21  False  False  False  False   True"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.isna(df1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stats 对数据进行统计分析\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-18    0.996454\n",
       "2019-09-19    0.995812\n",
       "2019-09-20    1.203546\n",
       "2019-09-21    1.494808\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求平均 不同维度\n",
    "df1.mean(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.996004\n",
       "B    0.991908\n",
       "C    1.003594\n",
       "F    2.000000\n",
       "E    1.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求平均\n",
    "df1.mean(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Series([1, 3, 5, np.nan, 6, 8,3,1,3,2,1,3], index=dates).shift(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-18    NaN\n",
       "2019-09-19    NaN\n",
       "2019-09-20    1.0\n",
       "2019-09-21    3.0\n",
       "2019-09-22    5.0\n",
       "2019-09-23    NaN\n",
       "2019-09-24    6.0\n",
       "2019-09-25    8.0\n",
       "2019-09-26    3.0\n",
       "2019-09-27    1.0\n",
       "2019-09-28    3.0\n",
       "2019-09-29    2.0\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>0.989936</td>\n",
       "      <td>0.990347</td>\n",
       "      <td>1.005533</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>0.984528</td>\n",
       "      <td>0.980366</td>\n",
       "      <td>1.018356</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>1.016322</td>\n",
       "      <td>1.003045</td>\n",
       "      <td>0.998361</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>0.993233</td>\n",
       "      <td>0.993873</td>\n",
       "      <td>0.992124</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>0.984321</td>\n",
       "      <td>0.998008</td>\n",
       "      <td>1.004511</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>0.994078</td>\n",
       "      <td>0.985869</td>\n",
       "      <td>0.990937</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>0.995265</td>\n",
       "      <td>1.009943</td>\n",
       "      <td>1.005325</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>0.997671</td>\n",
       "      <td>1.004346</td>\n",
       "      <td>1.010415</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>1.002895</td>\n",
       "      <td>1.021855</td>\n",
       "      <td>1.009793</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>1.001533</td>\n",
       "      <td>1.003061</td>\n",
       "      <td>1.012216</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>0.998256</td>\n",
       "      <td>0.984714</td>\n",
       "      <td>1.009659</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>1.020964</td>\n",
       "      <td>1.004581</td>\n",
       "      <td>1.005718</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C     F\n",
       "2019-09-18  0.989936  0.990347  1.005533   NaN\n",
       "2019-09-19  0.984528  0.980366  1.018356   1.0\n",
       "2019-09-20  1.016322  1.003045  0.998361   2.0\n",
       "2019-09-21  0.993233  0.993873  0.992124   3.0\n",
       "2019-09-22  0.984321  0.998008  1.004511   4.0\n",
       "2019-09-23  0.994078  0.985869  0.990937   5.0\n",
       "2019-09-24  0.995265  1.009943  1.005325   6.0\n",
       "2019-09-25  0.997671  1.004346  1.010415   7.0\n",
       "2019-09-26  1.002895  1.021855  1.009793   8.0\n",
       "2019-09-27  1.001533  1.003061  1.012216   9.0\n",
       "2019-09-28  0.998256  0.984714  1.009659  10.0\n",
       "2019-09-29  1.020964  1.004581  1.005718  11.0"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-18</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-19</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>0.016322</td>\n",
       "      <td>0.003045</td>\n",
       "      <td>-0.001639</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-2.006767</td>\n",
       "      <td>-2.006127</td>\n",
       "      <td>-2.007876</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-4.015679</td>\n",
       "      <td>-4.001992</td>\n",
       "      <td>-3.995489</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>-5.004735</td>\n",
       "      <td>-4.990057</td>\n",
       "      <td>-4.994675</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>-7.002329</td>\n",
       "      <td>-6.995654</td>\n",
       "      <td>-6.989585</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-26</th>\n",
       "      <td>-1.997105</td>\n",
       "      <td>-1.978145</td>\n",
       "      <td>-1.990207</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-27</th>\n",
       "      <td>0.001533</td>\n",
       "      <td>0.003061</td>\n",
       "      <td>0.012216</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-28</th>\n",
       "      <td>-2.001744</td>\n",
       "      <td>-2.015286</td>\n",
       "      <td>-1.990341</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-29</th>\n",
       "      <td>-0.979036</td>\n",
       "      <td>-0.995419</td>\n",
       "      <td>-0.994282</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C    F\n",
       "2019-09-18       NaN       NaN       NaN  NaN\n",
       "2019-09-19       NaN       NaN       NaN  NaN\n",
       "2019-09-20  0.016322  0.003045 -0.001639  1.0\n",
       "2019-09-21 -2.006767 -2.006127 -2.007876  0.0\n",
       "2019-09-22 -4.015679 -4.001992 -3.995489 -1.0\n",
       "2019-09-23       NaN       NaN       NaN  NaN\n",
       "2019-09-24 -5.004735 -4.990057 -4.994675  0.0\n",
       "2019-09-25 -7.002329 -6.995654 -6.989585 -1.0\n",
       "2019-09-26 -1.997105 -1.978145 -1.990207  5.0\n",
       "2019-09-27  0.001533  0.003061  0.012216  8.0\n",
       "2019-09-28 -2.001744 -2.015286 -1.990341  7.0\n",
       "2019-09-29 -0.979036 -0.995419 -0.994282  9.0"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.sub(s, axis=0)\n",
    "#自动的广播操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-18     1.005533\n",
       "2019-09-19     1.018356\n",
       "2019-09-20     2.000000\n",
       "2019-09-21     3.000000\n",
       "2019-09-22     4.000000\n",
       "2019-09-23     5.000000\n",
       "2019-09-24     6.000000\n",
       "2019-09-25     7.000000\n",
       "2019-09-26     8.000000\n",
       "2019-09-27     9.000000\n",
       "2019-09-28    10.000000\n",
       "2019-09-29    11.000000\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.max(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A     1.020964\n",
       "B     1.021855\n",
       "C     1.018356\n",
       "F    11.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.max(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.984321\n",
       "B    0.980366\n",
       "C    0.990937\n",
       "F    1.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.min(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A     0.036643\n",
       "B     0.041489\n",
       "C     0.027419\n",
       "F    10.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.apply(lambda x: x.max() - x.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-18    0.015597\n",
       "2019-09-19    0.037990\n",
       "2019-09-20    1.001639\n",
       "2019-09-21    2.007876\n",
       "2019-09-22    3.015679\n",
       "2019-09-23    4.014131\n",
       "2019-09-24    5.004735\n",
       "2019-09-25    6.002329\n",
       "2019-09-26    6.997105\n",
       "2019-09-27    7.998467\n",
       "2019-09-28    9.015286\n",
       "2019-09-29    9.995419\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_frame.apply(lambda x: x.max() - x.min(), axis = 1)\n",
    "#apply 默认每一列，也可以设置 axis = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Merge\n",
    "\n",
    "pandas提供了各种工具，可以在连接/合并类型操作的情况下，轻松地将Series和DataFrame对象与索引和关系代数功能的各种设置逻辑组合在一起。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(10, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.062130</td>\n",
       "      <td>-0.175628</td>\n",
       "      <td>0.159755</td>\n",
       "      <td>1.739656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.209627</td>\n",
       "      <td>-1.805313</td>\n",
       "      <td>-1.320563</td>\n",
       "      <td>-1.291183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.037564</td>\n",
       "      <td>-1.415054</td>\n",
       "      <td>-0.007676</td>\n",
       "      <td>-1.622740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.065259</td>\n",
       "      <td>-1.652177</td>\n",
       "      <td>0.620343</td>\n",
       "      <td>0.916965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.359694</td>\n",
       "      <td>1.583328</td>\n",
       "      <td>-0.096927</td>\n",
       "      <td>-1.322966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.552277</td>\n",
       "      <td>-1.226365</td>\n",
       "      <td>1.622037</td>\n",
       "      <td>0.859225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.601195</td>\n",
       "      <td>-0.000273</td>\n",
       "      <td>-1.063575</td>\n",
       "      <td>-1.046511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.417110</td>\n",
       "      <td>0.368958</td>\n",
       "      <td>-0.886600</td>\n",
       "      <td>-0.954898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.362485</td>\n",
       "      <td>-0.588403</td>\n",
       "      <td>0.450801</td>\n",
       "      <td>0.303764</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.352149</td>\n",
       "      <td>-0.688292</td>\n",
       "      <td>-1.264811</td>\n",
       "      <td>0.512458</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0  1.062130 -0.175628  0.159755  1.739656\n",
       "1 -0.209627 -1.805313 -1.320563 -1.291183\n",
       "2 -0.037564 -1.415054 -0.007676 -1.622740\n",
       "3 -0.065259 -1.652177  0.620343  0.916965\n",
       "4  0.359694  1.583328 -0.096927 -1.322966\n",
       "5  1.552277 -1.226365  1.622037  0.859225\n",
       "6  0.601195 -0.000273 -1.063575 -1.046511\n",
       "7  0.417110  0.368958 -0.886600 -0.954898\n",
       "8 -0.362485 -0.588403  0.450801  0.303764\n",
       "9  1.352149 -0.688292 -1.264811  0.512458"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "#break it into pieces\n",
    "\n",
    "pieces = [df[:3], df[3:7], df[7:]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.062130</td>\n",
       "      <td>-0.175628</td>\n",
       "      <td>0.159755</td>\n",
       "      <td>1.739656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.209627</td>\n",
       "      <td>-1.805313</td>\n",
       "      <td>-1.320563</td>\n",
       "      <td>-1.291183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.037564</td>\n",
       "      <td>-1.415054</td>\n",
       "      <td>-0.007676</td>\n",
       "      <td>-1.622740</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0  1.062130 -0.175628  0.159755  1.739656\n",
       "1 -0.209627 -1.805313 -1.320563 -1.291183\n",
       "2 -0.037564 -1.415054 -0.007676 -1.622740"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pieces[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.062130</td>\n",
       "      <td>-0.175628</td>\n",
       "      <td>0.159755</td>\n",
       "      <td>1.739656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.209627</td>\n",
       "      <td>-1.805313</td>\n",
       "      <td>-1.320563</td>\n",
       "      <td>-1.291183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.037564</td>\n",
       "      <td>-1.415054</td>\n",
       "      <td>-0.007676</td>\n",
       "      <td>-1.622740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.065259</td>\n",
       "      <td>-1.652177</td>\n",
       "      <td>0.620343</td>\n",
       "      <td>0.916965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.359694</td>\n",
       "      <td>1.583328</td>\n",
       "      <td>-0.096927</td>\n",
       "      <td>-1.322966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.552277</td>\n",
       "      <td>-1.226365</td>\n",
       "      <td>1.622037</td>\n",
       "      <td>0.859225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.601195</td>\n",
       "      <td>-0.000273</td>\n",
       "      <td>-1.063575</td>\n",
       "      <td>-1.046511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.417110</td>\n",
       "      <td>0.368958</td>\n",
       "      <td>-0.886600</td>\n",
       "      <td>-0.954898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.362485</td>\n",
       "      <td>-0.588403</td>\n",
       "      <td>0.450801</td>\n",
       "      <td>0.303764</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.352149</td>\n",
       "      <td>-0.688292</td>\n",
       "      <td>-1.264811</td>\n",
       "      <td>0.512458</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0  1.062130 -0.175628  0.159755  1.739656\n",
       "1 -0.209627 -1.805313 -1.320563 -1.291183\n",
       "2 -0.037564 -1.415054 -0.007676 -1.622740\n",
       "3 -0.065259 -1.652177  0.620343  0.916965\n",
       "4  0.359694  1.583328 -0.096927 -1.322966\n",
       "5  1.552277 -1.226365  1.622037  0.859225\n",
       "6  0.601195 -0.000273 -1.063575 -1.046511\n",
       "7  0.417110  0.368958 -0.886600 -0.954898\n",
       "8 -0.362485 -0.588403  0.450801  0.303764\n",
       "9  1.352149 -0.688292 -1.264811  0.512458"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat(pieces)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SQL style Join"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})\n",
    "\n",
    "right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval  rval\n",
       "0  foo     1     4\n",
       "1  foo     1     5\n",
       "2  foo     2     4\n",
       "3  foo     2     5"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left, right, on='key')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "left = pd.DataFrame({'key': ['foo', 'bar'], 'lval': [1, 2]})\n",
    "\n",
    "right = pd.DataFrame({'key': ['foo', 'bar'], 'rval': [4, 5]})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval  rval\n",
       "0  foo     1     4\n",
       "1  bar     2     5"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left, right, on='key')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.813085</td>\n",
       "      <td>0.488639</td>\n",
       "      <td>-0.395957</td>\n",
       "      <td>-2.817511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.890615</td>\n",
       "      <td>-0.202613</td>\n",
       "      <td>-0.396441</td>\n",
       "      <td>1.116092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.448051</td>\n",
       "      <td>-0.639552</td>\n",
       "      <td>-0.988499</td>\n",
       "      <td>0.451174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.360105</td>\n",
       "      <td>0.501142</td>\n",
       "      <td>-0.325823</td>\n",
       "      <td>-0.304948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.405415</td>\n",
       "      <td>0.285457</td>\n",
       "      <td>-1.072688</td>\n",
       "      <td>0.723796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.246270</td>\n",
       "      <td>-2.138887</td>\n",
       "      <td>0.857017</td>\n",
       "      <td>0.292886</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.771561</td>\n",
       "      <td>-1.661360</td>\n",
       "      <td>-1.150699</td>\n",
       "      <td>-0.409972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.558314</td>\n",
       "      <td>1.311026</td>\n",
       "      <td>1.092720</td>\n",
       "      <td>-1.375962</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D\n",
       "0 -0.813085  0.488639 -0.395957 -2.817511\n",
       "1  2.890615 -0.202613 -0.396441  1.116092\n",
       "2  0.448051 -0.639552 -0.988499  0.451174\n",
       "3 -0.360105  0.501142 -0.325823 -0.304948\n",
       "4 -2.405415  0.285457 -1.072688  0.723796\n",
       "5  0.246270 -2.138887  0.857017  0.292886\n",
       "6  0.771561 -1.661360 -1.150699 -0.409972\n",
       "7 -0.558314  1.311026  1.092720 -1.375962"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(8, 4), columns=['A', 'B', 'C', 'D'])\n",
    "s = df.loc[0]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.append(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.813085</td>\n",
       "      <td>0.488639</td>\n",
       "      <td>-0.395957</td>\n",
       "      <td>-2.817511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.890615</td>\n",
       "      <td>-0.202613</td>\n",
       "      <td>-0.396441</td>\n",
       "      <td>1.116092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.448051</td>\n",
       "      <td>-0.639552</td>\n",
       "      <td>-0.988499</td>\n",
       "      <td>0.451174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.360105</td>\n",
       "      <td>0.501142</td>\n",
       "      <td>-0.325823</td>\n",
       "      <td>-0.304948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.405415</td>\n",
       "      <td>0.285457</td>\n",
       "      <td>-1.072688</td>\n",
       "      <td>0.723796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.246270</td>\n",
       "      <td>-2.138887</td>\n",
       "      <td>0.857017</td>\n",
       "      <td>0.292886</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.771561</td>\n",
       "      <td>-1.661360</td>\n",
       "      <td>-1.150699</td>\n",
       "      <td>-0.409972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.558314</td>\n",
       "      <td>1.311026</td>\n",
       "      <td>1.092720</td>\n",
       "      <td>-1.375962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.813085</td>\n",
       "      <td>0.488639</td>\n",
       "      <td>-0.395957</td>\n",
       "      <td>-2.817511</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D\n",
       "0 -0.813085  0.488639 -0.395957 -2.817511\n",
       "1  2.890615 -0.202613 -0.396441  1.116092\n",
       "2  0.448051 -0.639552 -0.988499  0.451174\n",
       "3 -0.360105  0.501142 -0.325823 -0.304948\n",
       "4 -2.405415  0.285457 -1.072688  0.723796\n",
       "5  0.246270 -2.138887  0.857017  0.292886\n",
       "6  0.771561 -1.661360 -1.150699 -0.409972\n",
       "7 -0.558314  1.311026  1.092720 -1.375962\n",
       "0 -0.813085  0.488639 -0.395957 -2.817511"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A   -0.813085\n",
       "B    0.488639\n",
       "C   -0.395957\n",
       "D   -2.817511\n",
       "Name: 0, dtype: float64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Grouping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',\n",
    "     'foo', 'bar', 'foo', 'foo'],\n",
    "                'B': ['one', 'one', 'two', 'three',\n",
    "                        'two', 'two', 'one', 'three'],\n",
    "               'C': np.random.randn(8),\n",
    "                 'D': np.random.randn(8)})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bar</th>\n",
       "      <td>2.381716</td>\n",
       "      <td>-0.87077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>foo</th>\n",
       "      <td>-2.660868</td>\n",
       "      <td>1.62528</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            C        D\n",
       "A                     \n",
       "bar  2.381716 -0.87077\n",
       "foo -2.660868  1.62528"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('A').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>1.560562</td>\n",
       "      <td>0.650416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>1.805622</td>\n",
       "      <td>-0.237838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.984468</td>\n",
       "      <td>-1.283348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>1.437385</td>\n",
       "      <td>2.131087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>-0.704039</td>\n",
       "      <td>-0.383192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-3.394215</td>\n",
       "      <td>-0.122615</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  C         D\n",
       "A   B                        \n",
       "bar one    1.560562  0.650416\n",
       "    three  1.805622 -0.237838\n",
       "    two   -0.984468 -1.283348\n",
       "foo one    1.437385  2.131087\n",
       "    three -0.704039 -0.383192\n",
       "    two   -3.394215 -0.122615"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['A', 'B']).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Time series\n",
    "pandas has simple, powerful, and efficient functionality for performing resampling operations during frequency conversion (e.g., converting secondly data into 5-minutely data). This is extremely common in, but not limited to, financial applications. See the Time Series section.https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "rng = pd.date_range('1/1/2012', periods=100, freq='S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-01-01 00:00:00', '2012-01-01 00:00:01',\n",
       "               '2012-01-01 00:00:02', '2012-01-01 00:00:03',\n",
       "               '2012-01-01 00:00:04', '2012-01-01 00:00:05',\n",
       "               '2012-01-01 00:00:06', '2012-01-01 00:00:07',\n",
       "               '2012-01-01 00:00:08', '2012-01-01 00:00:09',\n",
       "               '2012-01-01 00:00:10', '2012-01-01 00:00:11',\n",
       "               '2012-01-01 00:00:12', '2012-01-01 00:00:13',\n",
       "               '2012-01-01 00:00:14', '2012-01-01 00:00:15',\n",
       "               '2012-01-01 00:00:16', '2012-01-01 00:00:17',\n",
       "               '2012-01-01 00:00:18', '2012-01-01 00:00:19',\n",
       "               '2012-01-01 00:00:20', '2012-01-01 00:00:21',\n",
       "               '2012-01-01 00:00:22', '2012-01-01 00:00:23',\n",
       "               '2012-01-01 00:00:24', '2012-01-01 00:00:25',\n",
       "               '2012-01-01 00:00:26', '2012-01-01 00:00:27',\n",
       "               '2012-01-01 00:00:28', '2012-01-01 00:00:29',\n",
       "               '2012-01-01 00:00:30', '2012-01-01 00:00:31',\n",
       "               '2012-01-01 00:00:32', '2012-01-01 00:00:33',\n",
       "               '2012-01-01 00:00:34', '2012-01-01 00:00:35',\n",
       "               '2012-01-01 00:00:36', '2012-01-01 00:00:37',\n",
       "               '2012-01-01 00:00:38', '2012-01-01 00:00:39',\n",
       "               '2012-01-01 00:00:40', '2012-01-01 00:00:41',\n",
       "               '2012-01-01 00:00:42', '2012-01-01 00:00:43',\n",
       "               '2012-01-01 00:00:44', '2012-01-01 00:00:45',\n",
       "               '2012-01-01 00:00:46', '2012-01-01 00:00:47',\n",
       "               '2012-01-01 00:00:48', '2012-01-01 00:00:49',\n",
       "               '2012-01-01 00:00:50', '2012-01-01 00:00:51',\n",
       "               '2012-01-01 00:00:52', '2012-01-01 00:00:53',\n",
       "               '2012-01-01 00:00:54', '2012-01-01 00:00:55',\n",
       "               '2012-01-01 00:00:56', '2012-01-01 00:00:57',\n",
       "               '2012-01-01 00:00:58', '2012-01-01 00:00:59',\n",
       "               '2012-01-01 00:01:00', '2012-01-01 00:01:01',\n",
       "               '2012-01-01 00:01:02', '2012-01-01 00:01:03',\n",
       "               '2012-01-01 00:01:04', '2012-01-01 00:01:05',\n",
       "               '2012-01-01 00:01:06', '2012-01-01 00:01:07',\n",
       "               '2012-01-01 00:01:08', '2012-01-01 00:01:09',\n",
       "               '2012-01-01 00:01:10', '2012-01-01 00:01:11',\n",
       "               '2012-01-01 00:01:12', '2012-01-01 00:01:13',\n",
       "               '2012-01-01 00:01:14', '2012-01-01 00:01:15',\n",
       "               '2012-01-01 00:01:16', '2012-01-01 00:01:17',\n",
       "               '2012-01-01 00:01:18', '2012-01-01 00:01:19',\n",
       "               '2012-01-01 00:01:20', '2012-01-01 00:01:21',\n",
       "               '2012-01-01 00:01:22', '2012-01-01 00:01:23',\n",
       "               '2012-01-01 00:01:24', '2012-01-01 00:01:25',\n",
       "               '2012-01-01 00:01:26', '2012-01-01 00:01:27',\n",
       "               '2012-01-01 00:01:28', '2012-01-01 00:01:29',\n",
       "               '2012-01-01 00:01:30', '2012-01-01 00:01:31',\n",
       "               '2012-01-01 00:01:32', '2012-01-01 00:01:33',\n",
       "               '2012-01-01 00:01:34', '2012-01-01 00:01:35',\n",
       "               '2012-01-01 00:01:36', '2012-01-01 00:01:37',\n",
       "               '2012-01-01 00:01:38', '2012-01-01 00:01:39'],\n",
       "              dtype='datetime64[ns]', freq='S')"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100,)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01 00:00:00    15990\n",
       "2012-01-01 00:01:00     9369\n",
       "Freq: T, dtype: int32"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('1Min').sum()\n",
    "# 把时间统计的数据，变成 另一个时间段内统计的数据\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ploting\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1000,)\n"
     ]
    }
   ],
   "source": [
    "# 化曲线图\n",
    "ts = pd.Series(np.random.randn(1000), index = list(range(1000)))\n",
    "ts = ts.cumsum()\n",
    "print(ts.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x21324ba1a90>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
